package com.rf.richfitwheel.workflow.controller;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.rf.richfitwheel.admin.annotation.ModifyAnnotaionFactory;
import com.rf.richfitwheel.admin.annotation.UserOperateLog;
import com.rf.richfitwheel.common.contants.CommonServiceError;
import com.rf.richfitwheel.common.exception.BusinessException;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;
import com.rf.richfitwheel.workflow.entity.Delegation;
import com.rf.richfitwheel.workflow.service.DelegationService;


@RestController
@RequestMapping("/delegation")
//@CrossOrigin("http://localhost:8769/*") 
public class DelegationController {

	protected static final Logger log = LoggerFactory.getLogger(DelegationController.class);
	
    @Autowired
    private DelegationService delegationService;


    /**
     * 授权信息
     *
     * @throws Exception
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) throws Exception {
        Delegation delegation = delegationService.findById(id);
        return R.ok().put("delegation", delegation);
    }

    /**
     * 添加授权记录
     *
     * @param delegation
     * @return
     */
    @PostMapping("/addDelegation")
    @UserOperateLog(module = "工作流", methods = "addDelegation", description = "添加授权记录", functionNo = "104001", logType = "2")
    public R addDelegation(@RequestBody Delegation delegation) {
        try {
            delegationService.addDelegation(delegation);
            return R.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error("添加授权记录" + CommonServiceError.SERVICE_FAILURE.getMsg());
        }
    }

    /**
     * 修改授权记录
     */
    @PostMapping("/updateDelegation")
    @UserOperateLog(module = "工作流", methods = "updateDelegation", description = "修改授权记录", functionNo = "104002", logType = "2")
    public R updateDelegation(@RequestBody Delegation delegation) {
        ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "updateDelegation", "bussid", delegation.getId(), delegation.getClass());
        try {
            delegationService.updateDelegation(delegation);
            return R.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error("修改授权记录" + CommonServiceError.SERVICE_FAILURE.getMsg());
        }
    }

    /**
     * 查询授权列表
     */
    @GetMapping("/list")
    public R getList(@RequestParam Map<String, Object> params) {
        PageUtils page = delegationService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 删除授权列表
     */
    @UserOperateLog(module = "工作流", methods = "deleteDelegation", description = "删除授权列表", logType = "2")
    @PostMapping("/delete")
    public R deleteDelegation(@RequestBody String[] ids) {
        try {
            delegationService.deleteDelegation(ids);
            return R.ok();
        } catch (BusinessException e) {
            log.error(e.getMessage(), e);
            return R.error(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error("删除授权列表" + CommonServiceError.SERVICE_FAILURE.getMsg());
        }
    }

    @PostMapping("/sendAttachmentMail")
    public R send(String subject) {
        System.out.println(subject);
        return R.ok();
    }
}
